<?php
// +----------------------------------------------------------------------
// | TYCMS [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2016 http://www.rainfer.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: aliang <461321026@qq.com>
// +----------------------------------------------------------------------

vendor("alisms.AliSdk"); 

class Alisms
{
	private $method = 'GET';
	private $domain = 'dysmsapi.aliyuncs.com';		//短信接口
	private $region = "cn-hangzhou";
 
	private $config = [
        'PhoneNumbers'     => '',
        'SignName'         => '',  
        'TemplateCode'     => '', 
        'TemplateParam'    => '', 
        'OutId'            => '',
        'AccessKeyId'      => '', 
        'accessKeySecret'  => '', 
    ];
	
	

    public function __construct($config = []){
        $this->config($config);
    }
    
    /*读取配置*/
    public function config($config = [])
    {
    	/**
	     *发送短信
	     *@AccessKeyId      阿里云申请的 Access Key ID
	     *@AccessKeySecret  阿里云申请的 Access Key Secret
	    */
        if(config('think_sdk_sms.AccessKeyId')) $conf['AccessKeyId']=config('think_sdk_sms.AccessKeyId');
        if(config('think_sdk_sms.accessKeySecret')) $conf['accessKeySecret']=config('think_sdk_sms.accessKeySecret');
        if(config('think_sdk_sms.signName')) $conf['SignName']=config('think_sdk_sms.signName');
        if(config('think_sdk_sms.TemplateCode')) $conf['TemplateCode']=config('think_sdk_sms.TemplateCode');
        $this->config = array_merge($this->config, $conf);
        $this->config = array_merge($this->config, $config);
        if(!config('think_sdk_sms.sms_open')){
        	throw new Exception("短信系统尚未配置和未开启!");
        }else{
        	return $this;
        }
    
    }

    /**
	*发送短信
	*@mobile  目标手机号，多个手机号可以逗号分隔 
	*@code 短信模板的模板CODE
	*@ParamString  短信模板中的变量；,参数格式{“no”:”123456”}， 个人用户每个变量长度必须小于15个字符
	*/
	public function smsend($mobile,$ParamString,$code='')
    {
         
        //此处需要替换成自己的AK信息
        $accessKeyId     = $this->config['AccessKeyId'];
        $accessKeySecret = $this->config['accessKeySecret'];
        
        //短信API产品域名
        $domain = $this->domain;
        //暂时不支持多Region
        $region = $this->region;
        $product= 'Dysmsapi';
        
        include_once VENDOR_PATH.'/alisms/Dysmsapi/Request/V20170525/SendSmsRequest.php';
        include_once VENDOR_PATH.'/alisms/Dysmsapi/Request/V20170525/QuerySendDetailsRequest.php';
        
        //初始化访问的acsCleint
        $profile = \DefaultProfile::getProfile($region, $accessKeyId, $accessKeySecret);
        DefaultProfile::addEndpoint("cn-hangzhou", "cn-hangzhou", $product, $domain);
        $acsClient= new \DefaultAcsClient($profile);
        
        $request = new \Dysmsapi\Request\V20170525\SendSmsRequest;
        //必填-短信接收号码
        $request->setPhoneNumbers($mobile);
        //必填-短信签名
        $request->setSignName($this->config['SignName']);
        //必填-短信模板Code
        $request->setTemplateCode($this->config['TemplateCode']);
        //选填-假如模板中存在变量需要替换则为必填(JSON格式)
        $request->setTemplateParam($ParamString);
        //选填-发送短信流水号
        //$request->setOutId("1234");
        
        //发起访问请求
        $acsResponse = $acsClient->getAcsResponse($request);
        return $acsResponse;
        
       
	}

     


}

    